package cn.iocoder.yudao.module.promotion.dal.mysql.combination;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.product.CombinationProductPageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO;
import java.time.LocalDateTime;

import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationSkuPriceDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

/**
 * 拼团商品 Mapper
 *
 * @author HUIHUI
 */
@Mapper
public interface CombinationProductMapper extends BaseMapperX<CombinationProductDO> {

    default PageResult<CombinationProductDO> selectPage(CombinationProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CombinationProductDO>()
            .eqIfPresent(CombinationProductDO::getActivityId, reqVO.getActivityId())
            .eqIfPresent(CombinationProductDO::getSpuId, reqVO.getSpuId())
            .eqIfPresent(CombinationProductDO::getSkuId, reqVO.getSkuId())
            .eqIfPresent(CombinationProductDO::getActivityStatus, reqVO.getActivityStatus())
            .betweenIfPresent(CombinationProductDO::getActivityStartTime, reqVO.getActivityStartTime())
            .betweenIfPresent(CombinationProductDO::getActivityEndTime, reqVO.getActivityEndTime())
            .eqIfPresent(CombinationProductDO::getCombinationPrice, reqVO.getActivePrice())
            .betweenIfPresent(CombinationProductDO::getCreateTime, reqVO.getCreateTime())
            .orderByDesc(CombinationProductDO::getId));
    }

    default List<CombinationProductDO> selectListByActivityIds(Collection<Long> ids) {
        return selectList(CombinationProductDO::getActivityId, ids);
    }

    default List<CombinationProductDO> selectListBySpuIdAndDateTimeLt(Long spuId, LocalDateTime dateTime) {
        return selectList(new LambdaQueryWrapperX<CombinationProductDO>()
            .in(CombinationProductDO::getSpuId, spuId)
            .lt(CombinationProductDO::getActivityStartTime, dateTime)
            .gt(CombinationProductDO::getActivityEndTime, dateTime)// 开始时间 < 指定时间 < 结束时间，也就是说获取指定时间段的活动
            .eq(CombinationProductDO::getActivityStatus, CommonStatusEnum.ENABLE.getStatus())
            .eq(CombinationProductDO::getDeleted, false));
    }

    /**
     * 获取指定 spuId 的最低价格的sku列表
     * @param spuIds
     * @return
     */
    List<CombinationSkuPriceDO> selectMinPriceSkuListBySpuId(@Param("spuIds") List<Long> spuIds);

}
